﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZedGraph;
using System.Drawing;

namespace Demonstartor
{
    class FlightDrawer
    {
        public static void Draw(FlightValues FlightValues, ZedGraphControl ZG)
        {
            Dictionary<InvolvedParameter, Color> ParamColors = new Dictionary<InvolvedParameter, Color>();

            GraphPane pane = ZG.GraphPane;
            pane.CurveList.Clear();
            pane.GraphObjList.Clear();

            Random R = new Random();

            bool WrongDrawed = false;

            foreach (var v in FlightValues)
            {
                int IntCount = v.Value.Count;

                bool Wrong = false;

                Color CurColor = Color.FromArgb(R.Next(255), R.Next(255), R.Next(255));

                for (int i = 0; i < IntCount; i++)
                {
                    int VCount = v.Value[i].Count;

                    double[] xvalues = new double[VCount];
                    double[] yvalues = new double[VCount];

                    for (int j = 0; j < VCount; j++)
                    {
                        xvalues[j] = new XDate(v.Value[i].X[j]);

                        yvalues[j] = v.Value[i].Y[j];
                    }

                    string Name = v.Key.ToString();

                    if (i != 0)
                        Name += i.ToString();

                    LineItem CurCurve = pane.AddCurve(Name, xvalues, yvalues, Color.Black, SymbolType.None);

                    if (i != 0)
                        CurCurve.Label.IsVisible = false;

                    CurCurve.Color = CurColor;

                    if (Wrong)
                    {
                        CurCurve.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                    }

                    if (v.Key == InvolvedParameter.Density())
                    {
                        if (Wrong)
                            CurCurve.Line.Fill = new Fill(Color.FromArgb(100, Color.Red));
                        else
                            CurCurve.Line.Fill = new Fill(Color.FromArgb(30, CurColor));
                    }

                    if (Wrong && !WrongDrawed)
                    {
                        // P = new PolyObj(CurPPoints, Color.Red, Color.FromArgb(225, Color.Red));
                        //pane.GraphObjList.Add(P);
                        if (VCount != 0)
                        {
                            BoxObj Box = new BoxObj(xvalues[0], 1, xvalues[VCount - 1] - xvalues[0], 0, Color.FromArgb(50, 230, 145, 205),
                                Color.FromArgb(50, 230, 145, 205));
                            pane.GraphObjList.Add(Box);
                        }
                    }


                    Wrong = !Wrong;

                }
                WrongDrawed = true;
            }
            

            pane.XAxis.Type = AxisType.Date;
            pane.XAxis.Scale.Format = "HH:mm:ss";
            pane.XAxis.Title.IsVisible = false;
            pane.YAxis.Title.IsVisible = false;
            pane.Title.IsVisible = false;

            // !!! Установим формат для календарной оси 
            // http://msdn.microsoft.com/ru-ru/library/system.globalization.datetimeformatinfo.aspx
            //pane.XAxis.Scale.Format = "dd.MM.yyyy hh:mm:ss";

            // !!! Установим формат для числовой оси
            // http://msdn.microsoft.com/ru-ru/library/system.globalization.numberformatinfo.aspx
            // pane.YAxis.Scale.Min = 0;
            // pane.YAxis.Scale.Max = 1;

            // Вызываем метод AxisChange (), чтобы обновить данные об осях. 
            ZG.AxisChange();

            // Обновляем график
            ZG.Invalidate();

        }

        public static void Draw()
        {
                        /*
            crtValues.Series.Clear();

            foreach (var v in Values)
            {
                int clbPInd = clbDrawingParams.Items.IndexOf(v.Key);
                if (!clbDrawingParams.GetItemChecked(clbPInd))
                    continue;

                System.Windows.Forms.DataVisualization.Charting.Series Cur = crtValues.Series.Add(v.Key);
                Cur.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

                Cur.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTimeOffset;

                int VCount = v.Value.Count();
                for (int i = 0; i < VCount; i++)
                    Cur.Points.AddXY(v.Value[i].X, v.Value[i].Y);
            }

            foreach (var v in WrongValues)
            {
                int clbPInd = clbDrawingParams.Items.IndexOf(v.Key);
                if (!clbDrawingParams.GetItemChecked(clbPInd))
                    continue;

                System.Windows.Forms.DataVisualization.Charting.Series Cur = crtValues.Series.Add(v.Key + "Wrong");
                Cur.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                Cur.Color = Color.Red;
                Cur.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTimeOffset;

                int VCount = v.Value.Count();
                for (int i = 0; i < VCount; i++)
                    Cur.Points.AddXY(v.Value[i].X, v.Value[i].Y);
            }*/
        }
    }

}
